﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace CallCenter.Data.BizUtilities
{
    /// <summary>
    /// Summary description for WebMail
    /// </summary>
    public class WebMail
    {
        private string userName;
        private string host;
        private string password;
        private string smtp;
        private int port;
        private bool ssl;

        public WebMail()
        {
            //
            // TODO: Add constructor logic here
            //
            var smtp = new System.Net.Mail.SmtpClient();
            host = smtp.Host;
            ssl = smtp.EnableSsl;
            port = smtp.Port;


            var credential = new System.Net.Configuration.SmtpSection().Network;
            userName = credential.UserName;
            password = credential.Password;
        }

        public static bool getSMTPExistence()
        {
            return true;
        }

        public static void SendMail(string[] to, string[] cc, string subject, string body, string attachmentFilename)
        {
            string From = "msgresnav@gmail.com";// "resnav@resnav.com";
            string host = "smtp.gmail.com";// "smtp.bizmail.yahoo.com";
            string password = "ResnavPassword";
            int port = 587;
            string userName = "msgresnav@gmail.com";// "resnav@resnav.com";
            bool ssl = false;

            var smtp = new System.Net.Mail.SmtpClient();
            if (smtp != null)
            {
                host = smtp.Host;
                ssl = smtp.EnableSsl;
                port = smtp.Port;


                var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
                var credentials = smtpSection.Network;

                userName = credentials.UserName;
                password = credentials.Password;
            }

            MailMessage message = new MailMessage();
            smtp.Timeout = (60 * 1 * 1000);
            smtp.UseDefaultCredentials = false;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(userName, password);
            smtp.EnableSsl = true;
            smtp.Port = 587;

            if (to != null && cc != null)
            {
                message.From = new MailAddress(userName);
                message.Subject = subject;
                message.IsBodyHtml = true;
                message.Body = body;
                for (int i = 0; i < to.Length; i++)
                    message.To.Add(new MailAddress(to[i]));


                for (int i = 0; i < cc.Length; i++)
                {
                    if (!cc[i].Equals("none"))
                        message.CC.Add(new MailAddress(cc[i]));
                }


                if (attachmentFilename != null)
                    message.Attachments.Add(new Attachment(attachmentFilename));

                //smtpClient.Send(message);
                try
                {
                    smtp.Send(message);
                }
                catch
                {

                }
            }
        }

    }
}
